import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—05—21
 * Time: 19:30
 */
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
/*        if(this.age > o.age) {
            return 1;
        }else if(this.age == o.age) {
            return 0;
        }else {
            return -1;
        }*/
        return this.age - o.age;
    }
}
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class ScoreComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score - o2.score);
    }
}
class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class demo01 {
    public static void sort(Comparable[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = array.length - 1 - i; j > i; j--) {
                if (array[j].compareTo(array[j+1]) > 0) {
                    // 说明顺序不符合要求, 交换两个变量的位置
                    Comparable tmp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhang san",22,60);
        students[1] = new Student("li si",21,90);
        students[2] = new  Student("wang wu",21,80);
        System.out.println("排序前" + Arrays.toString(students));
        sort(students);
/*        AgeComparator ageComparator =new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        Arrays.sort(students,scoreComparator);*/
        System.out.println("排序后" + Arrays.toString(students));
    }

/*    public static void main2(String[] args) {
        Student student = new Student("zhang san",20,60);
        Student student1 = new Student("li si",21,70);

        AgeComparator ageComparator =new AgeComparator();
        int ret = ageComparator.compare(student,student1);
        System.out.println(ret);

        ScoreComparator scoreComparator = new ScoreComparator();
        int ret2 = scoreComparator.compare(student,student1);
        System.out.println("分数比较" + ret2);
    }*/
/*    public static void main(String[] args) {
        Student student = new Student("zhang san",20,60);
        Student student1 = new Student("li si",21,70);
        Student student2 = new  Student("wang wu",21,80);
        if(student.compareTo(student1) > 0){
            System.out.println("student > student1");
        }else if (student.compareTo(student1) == 0){
            System.out.println("student == student1");
        }else {
            System.out.println("student < student1");
        }
    }*/
}
